#include<iostream>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
long long a,b,c,d;
string e,f;
map<string,long long> g;
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>a;
	for(int x=1;x<=a;x++){
		long long i=1,j=0,l=0;
		bool k=false;
		cin>>e>>f;
		c++;
		for(int y=1;y<f.size();y++){
			if(f[y]>'9'||f[y]<'0'){
				l++;
				if(l>4||(l<4&&f[y]!='.')||(l==4&&f[y]!=':')){
		    		cout<<"ERR\n";
		    		k=true;
		    		break;
		    	}
			}
			if((f[y]>'9'||f[y]<'0')&&(f[y-1]>'9'||f[y-1]<'0')){
				cout<<"ERR\n";
				k=true;
				break;
			}
		}
		if(k) continue;
		if(l<4){
			cout<<"ERR\n";
			continue;
		}
		for(int y=1;y<f.size()-1;y++){
			if((f[y-1]<'0'||f[y-1]>'9')&&f[y]=='0'&&(f[y+1]>='1'&&f[y+1]<='9')){
				cout<<"ERR\n";
				k=true;
				break;
			}
		}
		if(k) continue;
		for(int y=0;y<f.size();y++){
			if(f[y]>='0'&&f[y]<='9'){
				j*=10;
				j+=f[y]-'0';
				if(i<=4){
					if(j>255||j<0){
						cout<<"ERR\n";
						k=true;
						break;
					}
				}else{
					if(j>65535||j<0){
						cout<<"ERR\n";
						k=true;
						break;
					}
				}
			}else{
				if((i<=3&&f[y]!='.')||(i==4&&f[y]!=':')||f[y+1]<'0'||f[y+1]>'9'||i>4){
					cout<<"ERR\n";
					k=true;
					break;
				}
				j=0;
				i++;
			}
		}
		if(k) continue;
		if(e=="Server"){
			if(g[f]>0){
				cout<<"FAIL\n";
				continue;
			}
			g[f]=c;
			cout<<"OK\n";
		}else{
			if(g[f]>0) cout<<g[f]<<'\n';
			else cout<<"FAIL\n";
		}
	}
}
